package com.example.viewdemo

import android.os.Bundle
import android.util.Log
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.setPadding
import androidx.recyclerview.widget.RecyclerView
import com.drake.brv.utils.setup
import com.example.viewdemo.databinding.ActivityFlexBoxManagerBinding
import com.example.viewdemo.databinding.ItemTextBinding
import com.google.android.flexbox.FlexboxLayoutManager

class FlexBoxManagerActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityFlexBoxManagerBinding.inflate(layoutInflater)
        setContentView(binding.root)

        repeat(5) { times ->
            TextView(this).apply {
                text = "item ${times + 1}"
                setPadding(20)
            }.let {
                binding.rootView.addView(it)
            }

            RecyclerView(this).setup {
                Log.d("FlexBoxManagerActivity", "onCreate: ${it.layoutParams}")
                it.layoutParams = LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
                )
                it.layoutManager = FlexboxLayoutManager(this@FlexBoxManagerActivity)
                binding.rootView.addView(it)
                addType<String>(R.layout.item_text)
                onBind {
                    val item = getModel<String>()
                    val itemBinding = getBinding<ItemTextBinding>()
                    itemBinding.textView.text = item
                }
            }.models = List(150) { "item $it" }
        }
    }
}